package com.orange.notificationsummary.loader;

import java.util.TreeMap;

import com.orange.notificationsummary.SummaryActivity;
import com.orange.notificationsummary.utils.LogHelper;

public class LoaderController {
	private static final String						TAG					= "LoaderController";

	private volatile BasicLoader					mExecutingloader	= null;
	private volatile TreeMap<String, BasicLoader>	mPendingLoaders		= new TreeMap<String, BasicLoader>();

	public static final int							MISSCALL_LOADER		= 1;
	public static final int							UNREADMSG_LOADER	= 2;
	public static final int							VOICEMAIL_LOADER	= 3;

	private volatile MissCallLoader					mMissCallLoader		= null;
	private volatile UnreadMsgLoader				mUnreadMsgLoader	= null;
	private volatile VoiceMailLoader				mVoiceMailLoader	= null;

	public void startMissCallLoader(SummaryActivity hostActivity) {
		if (mMissCallLoader == null) {
			mMissCallLoader = new MissCallLoader(hostActivity) {
				@Override
				protected void onFinish() {
					// TODO Auto-generated method stub
					super.onFinish();
					finishLoaderExecution();
					processPendingLoaders();
					mMissCallLoader = null;
				}
			};
		}
		tryToExecuteLoader(mMissCallLoader);
	}

	public void startUnreadMsgLoader(SummaryActivity hostActivity) {
		if (mUnreadMsgLoader == null) {
			mUnreadMsgLoader = new UnreadMsgLoader(hostActivity) {
				@Override
				protected void onFinish() {
					// TODO Auto-generated method stub
					super.onFinish();
					finishLoaderExecution();
					processPendingLoaders();
					mUnreadMsgLoader = null;

				}
			};
		}
		tryToExecuteLoader(mUnreadMsgLoader);
	}

	public void startVoiceMailLoader(SummaryActivity hostActivity) {
		if (mVoiceMailLoader == null) {
			mVoiceMailLoader = new VoiceMailLoader(hostActivity) {
				@Override
				protected void onFinish() {
					// TODO Auto-generated method stub
					super.onFinish();
					finishLoaderExecution();
					processPendingLoaders();
					mVoiceMailLoader = null;

				}
			};
		}
		tryToExecuteLoader(mVoiceMailLoader);
	}

	public void reset() {
		mPendingLoaders.clear();
		mExecutingloader = null;
		mMissCallLoader = null;
		mUnreadMsgLoader = null;
		mVoiceMailLoader = null;

	}

	public void abortLoaders() {
		mPendingLoaders.clear();
		if (mExecutingloader != null && !mExecutingloader.isFinised()) {
			mExecutingloader.stop();
		}
	}

	private void tryToExecuteLoader(BasicLoader loader) {
		if (loader == null) {
			return;
		}
		if (mExecutingloader != null && !mExecutingloader.isFinised()) {
			pendingLoader(loader);
			return;
		}
		executeLoader(loader);
	}

	private void pendingLoader(BasicLoader loader) {
		if (loader == null) {
			return;
		}

		String key = Integer.toString(loader.getLoaderType());

		mPendingLoaders.remove(key);
		mPendingLoaders.put(key, loader);
	}

	private void executeLoader(BasicLoader loader) {
		if (loader == null) {
			return;
		}

		LogHelper.d(
				TAG,
				"executeLoader. "
						+ String.format("executeLoader(): loader(%s)", loader));

		mExecutingloader = loader;
		loader.triggerStart();
	}

	private void finishLoaderExecution() {
		mExecutingloader = null;
	}

	private void processPendingLoaders() {
		if (!mPendingLoaders.isEmpty()) {
			String firstKey = mPendingLoaders.firstKey();
			executeLoader(mPendingLoaders.remove(firstKey));
		}
	}
}
